package com.wiim.ui;

import java.io.File;
import java.util.HashMap;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView;
import android.widget.Toast;

import com.wiim.common.ConstantValue;
import com.wiim.common.Utilities;
import com.wiim.interfaceapp.onImageSet;
import com.wiim.libs.SessionManager;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.R;

public class ThongTinTkFragment extends Fragment implements OnClickListener, onImageSet {
	private ImageView imgAvartar;
	private TextView tvFullname, tvMoney;
	private SessionManager session;
	private Button btnBanbe;
	private Button btnLichsu;
	private Button btnDieuchinh;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.thongtintk_layout, container, false);
		initFeld(view);
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		initData();
		Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

		if (isSDPresent) {
			Log.v("HAI", "yes");
		} else {
			Log.v("HAI", "no");
			imgAvartar.setEnabled(false);
		}
	}

	private void initFeld(View view) {
		session = new SessionManager(getActivity());
		imgAvartar = (ImageView) view.findViewById(R.id.imgAvartar);
		tvFullname = (TextView) view.findViewById(R.id.tvFullname);
		tvMoney = (TextView) view.findViewById(R.id.tvMoney);
		btnBanbe = (Button) view.findViewById(R.id.btnBanbe);
		btnLichsu = (Button) view.findViewById(R.id.btnLichsu);
		btnDieuchinh = (Button) view.findViewById(R.id.btnDieuchinh);
		imgAvartar.setOnClickListener(this);
		btnBanbe.setOnClickListener(this);
		btnLichsu.setOnClickListener(this);
		btnDieuchinh.setOnClickListener(this);
		// LoginActivity loginac = new LoginActivity();
		// tvName.setText(loginac.getUserModel().getUsername());
		tvFullname.setText(new LoginActivity().getUserModel().Fullname);
		tvMoney.setText(new LoginActivity().getUserModel().getRewardPoints() + " " + getString(R.string.str_diem));

	}

	private void initData() {
		try {
			// get user data from session
			HashMap<String, String> user = session.getUserDetails();

			// name
			String uri = user.get(SessionManager.KEY_URI);

			// email
			String path = user.get(SessionManager.KEY_PATH);
			Uri myUri = Uri.parse(uri);
			Bitmap bitmap = null;
			if (uri != null) {
				bitmap = ((CustomFragmentActivity) getActivity()).getBitmapFromURI(getActivity(), myUri);
			} else {
				File imgFile = new File(path);
				bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
			}
			Bitmap reSize = Utilities.getResizedBitmap(bitmap, 210, 210);
			imgAvartar.setImageBitmap(reSize);

		} catch (Exception e) {
			// TODO: handle exception
		}

		// tvMoney.setText(getString(R.string.str_diem) + (new
		// LoginActivity().getUserModel().RewardPoint==null?"0":new
		// LoginActivity().getUserModel().RewardPoint));
	}

	@Override
	public void setimage(Uri selectedImageUri, String path) {
		// TODO Auto-generated method stub
		try {
			// this.strPhotoType =
			// path.toString().substring(path.toString().lastIndexOf(".") + 1,
			// path.toString().length());
			Bitmap bitmap = null;
			if (selectedImageUri != null) {
				bitmap = ((CustomFragmentActivity) getActivity()).getBitmapFromURI(getActivity(), selectedImageUri);
			} else {
				File imgFile = new File(path);
				bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
			}
			Bitmap reSize = Utilities.getResizedBitmap(bitmap, 210, 210);
			imgAvartar.setImageBitmap(reSize);
			session.saveLink(selectedImageUri.toString(), path);
			// this.strPhoto = ((CustomFragmentActivity)
			// getActivity()).getStringFromBitmap(reSize, this.strPhotoType);
		} catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		if (v == imgAvartar) {
			((CustomFragmentActivity) getActivity()).getPicture();
		}
		if (v == btnBanbe) {
			Toast.makeText(getActivity(), "Comming soon..", Toast.LENGTH_SHORT).show();
		}
		if (v == btnLichsu) {
			// Intent i = new Intent(getActivity(), LichSuFragment.class);
			// startActivity(i);
			((CustomFragmentActivity) getActivity()).replaceFragment(ConstantValue.HISTORY_FRAGMENT, false);
		}
		if (v == btnDieuchinh) {
			Toast.makeText(getActivity(), "Comming soon..", Toast.LENGTH_SHORT).show();
		}

	}

}
